package com.monster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.monster.mapper.OrderSettingMapper;
import com.monster.pojo.OrderSetting;
import com.monster.service.OrderSettingService;
import com.monster.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    //将行对象
    @Override
    public Boolean importOrderSettings(List<String[]> orderSettingList) {
        //将文件的对象转化为list的集合
        List<OrderSetting> orderSettings = toOrderSetting(orderSettingList);
        //将每个对象循环遍历，然后获取传入的每个日期，进行查询，如果有相同的就进行将id赋值，是saveOrUpdateBatch进行的是update方法
        for (OrderSetting orderSetting : orderSettings) {
            QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<OrderSetting>();
            orderSettingQueryWrapper.eq("orderdate", orderSetting.getOrderDate());
            OrderSetting selectOne = baseMapper.selectOne(orderSettingQueryWrapper);
            if(selectOne != null){
                orderSetting.setId(selectOne.getId());
            }
        }
        saveOrUpdateBatch(orderSettings);
        return true;
    }
    //
    @Override
    public Map findSettingData(int year, int month) {
//        String beginDate = year+"-"+month+"-1";
//        String endDate = year+"-"+month+"-31";
//        List<OrderSetting> list = baseMapper.findSettingDataByYearAndMonth(beginDate,endDate);
        QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<>();
        QueryWrapper<OrderSetting> orderdate = orderSettingQueryWrapper.likeRight("ORDERDATE", year + "-" + month);
        List<OrderSetting> list = list(orderdate);
        Map<String, Map<String, Integer>> stringMapHashMap = new HashMap<>();
        for (OrderSetting orderSetting : list) {
        Map<String, Integer> voMap = new HashMap<>();
        voMap.put("number",orderSetting.getNumber());
        voMap.put("reservations",orderSetting.getReservations());
        String day = DateUtils.parseDate2String(orderSetting.getOrderDate(), "yyyy-MM-dd");
        stringMapHashMap.put(day,voMap);
        }
        return stringMapHashMap;
    }

    @Override
    public Boolean updateNumberByOrderdate(int number, String orderdate) {
        baseMapper.updateNumberByOrderdate(number,orderdate);
        return null;
    }

    //自定义一个方法为了使可以将传入的string集合对象转化为一个OrderSetting集合对象
    private List<OrderSetting> toOrderSetting(List<String[]> orderSettingList){
        List<OrderSetting> orderSettings = new ArrayList<>();
        if(orderSettings.size() < 0){
            return null;
        }else{
            for (String[] strings : orderSettingList) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setReservations(0);
                System.out.println(strings[0]+"~~~"+strings[1]);
                orderSetting.setNumber(Integer.parseInt(strings[1].trim()));
                orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0],"yyyy/MM/dd"));
                orderSettings.add(orderSetting);
            }
        }
        return orderSettings;
    }
}
